﻿@using viBook.Models
@model IEnumerable<ReportOnChapterComment>
@{
    ViewBag.Title = "Chapter Comments Report";
    ViewBag.Menu = "Report";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
@(Html.Telerik().Grid(Model)
               .Name("report-comment")
               .DataKeys(keys => keys.Add(c => c.Id))
               .Columns(columns =>
                            {
                                columns.Bound(c => c.Id).Width(50);
                                columns.Bound(c => c.ReportedDate).Format("{0:dd/MM/yyyy}").Width(80);
                                columns.Bound(c => c.Id)
                                    .Template(
                                        @<text>
                                        @if (!item.IsApproved)
                                        {
                                            <a href='@Url.Action("Details", "Chapters", new { id = item.ChapterComment.ChapterId })'>Go to chapter</a>
                                        }
                                        </text>
                                    )
                                    .ClientTemplate(string.Format("<a href='/Chapters/Details/<#= ChapterComment.ChapterId #>'>Go to chapter</a>"))
                                    .Title("Chapter")
                                    .Width(80);
                                columns.Bound(c => c.ChapterComment.Content)
                                    .Width(250);
                                columns.Bound(c => c.Id)
                                    .Template(
                                        @<text> 
                                            <a class='t-button' href='javascript:approve(@item.Id)'>Approve</a>
                                            <a class='t-button' href='javascript:disapprove(@item.Id)'>Disapprove</a>
                                        </text>
                                    )
                                    .ClientTemplate(
                                        string.Format("<a class='t-button' href='javascript:approve(<#= Id #>)'>Approve</a>" +
                                                      "<a class='t-button' href='javascript:disapprove(<#= Id #>)'>Disapprove</a>")
                                    )
                                    .Title("Action")
                                    .Width(140);
                            })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Server().Select("ChapterCommentsReport", "Admins");
                    dataBinding.Ajax().Select("SelectChapterCommentReportAjax", "Admins").Enabled(true);
                })
                .Scrollable()
                .Sortable()
                .Pageable()
                .Filterable()
                .Footer(true)
        )
<script type="text/javascript">
    function approve(id) {
        $.ajax({
            url: '@Url.Action("ApproveChapterCommentReportAjax", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function (result) {
                var reportChapterCommentGrid = $('#report-comment').data('tGrid');
                reportChapterCommentGrid.rebind();
            }
        });
    }

    function disapprove(id) {
        $.ajax({
            url: '@Url.Action("DisapproveChapterCommentReportAjax", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function (result) {
                var reportChapterCommentGrid = $('#report-comment').data('tGrid');
                reportChapterCommentGrid.rebind();
            }
        });
    }
</script>